package com.fjwt.gz.service.activity.coupon.send;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.coupon.SendCouponBo;
import com.fjwt.gz.coupon.tlpay.TlpayCouponSendService;
import com.fjwt.gz.db.bo.CouponResultBo;
import com.fjwt.gz.db.entity.FansEntity;
import com.fjwt.gz.db.entity.FansPayEntity;
import com.fjwt.gz.db.entity.PayAuthEntity;
import com.fjwt.gz.db.entity.SysConfig;
import com.fjwt.gz.interfaces.ISendCouponService;
import com.fjwt.gz.service.impl.FansPayService;
import com.fjwt.gz.service.mapper.FansMapper;
import com.fjwt.gz.service.mapper.SysConfigMapper;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Slf4j
@Service
public class TlpaySendCouponService implements ISendCouponService {
    private static final Logger tlPayLogger = LoggerFactory.getLogger("tlPay");

    @Autowired
    private FansPayService fansPayService;
    @Autowired
    private TlpayCouponSendService tlpayCouponSendService;
    @Autowired
    private SysConfigMapper sysConfigMapper;
    @Autowired
    private FansMapper fansMapper;

    @Override
    public CouponResultBo couponSend(PayAuthEntity authEntity, Long fansId, @NonNull String payConfig, Long couponId) {

        tlPayLogger.info("通联发券，支付通道方出资信息：{}，fansId：{}，,卡券payConfig：{}",
                JSON.toJSONString(authEntity), fansId, payConfig);
        SendCouponBo sendCouponBo = new SendCouponBo();

        String openId = "";
        if (0 == authEntity.getIsCommSubject()) {
            LambdaQueryWrapper<FansPayEntity> gw = FansPayEntity.gw();
            gw.eq(FansPayEntity::getIfCode, authEntity.getIfCode());
            gw.eq(FansPayEntity::getFansId, fansId);
            FansPayEntity fansOpen = fansPayService.getOne(gw);
            if (Objects.isNull(fansOpen) || Objects.isNull(authEntity)) {
                throw new BizException("请用户先到活动详情页进行授权，然后再进行派发卡券！");
            }
            tlPayLogger.info("用户信息fansId:{},用户信息：{}", fansId, JSON.toJSONString(fansOpen));
            openId = fansOpen.getOpenId();
        } else {
            FansEntity fansEntity = fansMapper.selectById(fansId);
            if (Objects.isNull(fansEntity) || Objects.isNull(authEntity)) {
                throw new BizException("发券参数缺失！");
            }
            tlPayLogger.info("用户信息fansId:{},用户信息：{}", fansId, JSON.toJSONString(fansEntity));
            openId = fansEntity.getPayOpenId();
        }

        LambdaQueryWrapper<SysConfig> sysgw = SysConfig.gw();
        sysgw.eq(SysConfig::getConfigKey, "tlpay");
        SysConfig sysConfig = sysConfigMapper.selectOne(sysgw);

        sendCouponBo.setOpenId(openId);
        sendCouponBo.setStockId(payConfig);
        sendCouponBo.setAuthConfig(JSONObject.parseObject(sysConfig.getConfigVal()));
        JSONObject jsonObject = tlpayCouponSendService.couponSend(sendCouponBo);

        CouponResultBo couponResultBo = new CouponResultBo();
        couponResultBo.setCouponId(jsonObject.getString("couponId"));
        couponResultBo.setCouponThirdId(jsonObject.getString("value"));
        tlPayLogger.info("通联发券返回参数：{}", couponResultBo);
        return couponResultBo;
    }

}
